home *** CD-ROM | disk | FTP | other *** search
/ F1 Licenseware / F1 Licenseware - Volume 1.iso / disks / 050a.dms / 050a.adf / TEXTS / chapter17.txt < prev    next >
Text File  |  1992-02-26  |  8KB  |  263 lines

  1.                      The Absolute Beginners Guide To Amos
  2.                     -------------------------------------
  3.                               Chapter seventeen
  4.                               ------------------
  5.  
  6.                  The Answers to The Self Testing Quiz part 4:
  7.  
  8.                                 1. B     6. A
  9.                                 2. A     7. B
  10.                                 3. A     8. C
  11.                                 4. C     9. B
  12.                                 5. C    10. C
  13.  
  14. ===========================================================================
  15.  
  16. Now I would like to cover DIMensioned Arrays. I know it sounds frightening at
  17. first. I can remember trying to get to grips with it myself, but don`t
  18. be misled by the name of this command as it`s not as bad as it sounds.  
  19. It`s the concept that some people can find hard to grasp rather than the 
  20. usage.
  21.  
  22. THE CONCEPT OF ARRAYS:
  23. ----------------------
  24. Arrays are for manipulating large amounts of data or variables.  For example
  25. a league of 22 football teams could be grouped into an array called team$
  26. we would access a team like this:
  27.  
  28. TEAM$(0)
  29.  
  30. Where if the table was sorted the first team might be Arsenal or Aston Villa
  31. for example.  Similarly this can also apply to numerical variables we could 
  32. group together all of the money we spent in the last year on beer in an array
  33. called BEER we could then:
  34.  
  35. PRINT BEER (5)
  36. This would produce the figure we entered for day five.
  37.  
  38. Or team 5 from the TEAM$ array like this:
  39. PRINT TEAM$ (5) 
  40.  
  41. Notice the only difference (so far) with a string array and a numerical array
  42. is the $ sign.
  43.  
  44. SETTING UP AN ARRAY
  45. -------------------
  46.  
  47. OK so we have 22 teams from the English Premier League and we want to group
  48. these into an array called team$.  First of all we must reserve some 
  49. variable space for our array using the DIM command.
  50.  
  51. DIM TEAM$ (21)
  52.  
  53. DIM tells Amos we want to reserve space for an array called team$ which will
  54. contain 22 items.  Arrays always start at zero, so for 22 teams we would 
  55. count from 0 to 21.
  56. Amos knows it`s a string array as we used a $ after the variable team.
  57. If we had put DIM team (21) Amos would presume we wanted a numerical array.
  58.  
  59. Now then, how do we input the data? This is how:
  60.  
  61. DATA "SPURS","ASTON VILLA","CHELSEA","NORWICH"
  62. DATA "NEWCASTLE","SWINDON","SHEFF WED","Q.P.R"
  63.  
  64. etc. until we have typed our 22 teams in.  Notice the exact format,
  65.  
  66. DATA String in quotes, comma, String in quotes, comma etc.
  67.  
  68. If we were handling our beer numerical array we would enter the 
  69. numbers like this,
  70.  
  71. DATA 12,17,13,11,12
  72. DATA 22,54,11,0,9,7
  73. etc.
  74.  
  75. Almost the same except no quotes.
  76.  
  77.  
  78. We have DIMensioned a 22 slot array for our team names and 
  79. typed the names into DATA statements, but Amos still does not know
  80. what to do with all this stuff. At the moment Amos will DIMension your array
  81. and ignore the DATA.  We have to tell Amos to READ the DATA into the ARRAY.
  82.  
  83. FOR A=0 to 21      
  84.  
  85. READ TEAM$(A)
  86.  
  87. NEXT A
  88.  
  89. We must set up a FOR NEXT loop to read each team from the DATA list into the 
  90. array.  Because we used TEAM$ in the READ line Amos knows we wanted to READ 
  91. the DATA into our DIMmed array called TEAM$.
  92.  
  93.  
  94. The beer numerical array would be almost identical.
  95.  
  96. FOR A=0 to 364
  97.  
  98. READ beer(A)
  99.  
  100. NEXT A  
  101.  
  102.  
  103. Note: Often it is a good idea to put a RESTORE command before you 
  104.       READ the DATA in.  This sets the internal DATA pointer to the 
  105.       beginning of the list of DATA.  If you get an OUT OF DATA error then
  106.       RESTORE could solve problem. An example would be:
  107.  
  108.       RESTORE league
  109.  
  110.       league:
  111.       DATA "SPURS","CHELSEA" etc.
  112.  
  113.       The league: bit is just a label so we can tell Amos what DATA we want
  114.       to RESTORE because some programs will contain more than one set of 
  115.       DATA statements.
  116.  
  117.  
  118. So that is the painful part over the rest is plain sailing.  
  119. The DATA is now safely in the array and you could access it like this:
  120.  
  121. PRINT TEAM$(1)  
  122.  
  123. SPURS             <<< (This is what would be PRINTed on screen)
  124.  
  125.  
  126. PRINT TEAM$(2)
  127.  
  128. ASTON VILLA
  129.  
  130.  
  131. PRINT TEAM$ (3)
  132.  
  133. CHELSEA
  134.  
  135.  
  136. And the beer array,
  137.  
  138. PRINT beer(1)
  139.  
  140. 12
  141.  
  142.  
  143. PRINT beer(2)
  144.  
  145. 17
  146.  
  147.  
  148. PRINT beer (3)
  149.  
  150. 13
  151.  
  152.  
  153. But if we wanted to print all 22 of our teams on the screen, a neater and
  154. more practical  way would be to use a FOR NEXT loop similar to the one we 
  155. used to READ the DATA in.
  156.  
  157. FOR A=0 TO 21
  158. PRINT TEAM$(A)
  159. NEXT A
  160.  
  161. This would print all 22 team names on the screen in a whisker.  To do this to
  162. the beer array we could just substitute the 21 for 364 and TEAM$ for beer.
  163.  
  164. One of the many bonuses of using of arrays is that we can use the Amos SORT
  165. command, which is fast and saves a lot of coding. 
  166.  
  167. If we want to SORT our array all we do is this:
  168.   
  169. SORT TEAM$ (0)
  170.  
  171. The zero is the start of our array.  For the beer array it`s,
  172.  
  173. SORT beer (0)
  174.  
  175. And the teams will all be sorted in alphabetical order and the beer numbers
  176. will be sorted in ascending order in an instant and all in one command.
  177.  
  178. There is a lot more to do with arrays than this little tutorial
  179. could possibly cover and I don`t want to go any further at this stage but 
  180. you now have enough knowledge to use simple arrays in your programs.
  181.  
  182. Before we continue on to a new subject you may want to take a look at
  183. EXAMPLE17.Amos which uses our team$ idea.  The example program also covers
  184. the next part of this chapter which is all to do with PROCEDURES.  While
  185. DIM, DATA and such like are still fresh in your mind take a browse at it.
  186.  
  187.  
  188. PROCEDURES
  189. ----------
  190. Think of procedures as self contained programs a bit like subroutines.
  191. You can write a small part of your program and fold it out of sight into one
  192. line making your listings easier to read and easier to debug and program.
  193. Procedures are well worth taking the time to learn how to use and once you 
  194. get in the habit of using procedures you will always use them.
  195.  
  196. Here is a part of a program that we would probably want to use many times
  197. during the course of the program:
  198.  
  199. WHILE MOUSE KEY=0: WEND
  200.  
  201. Let`s presume there are five places in the program we need to use a mouse 
  202. wait (which is what this line does, remember?) We could have five separate 
  203. lines in the program we could use GOSUB or better we could put it in a
  204. PROCEDURE, this is how it`s done.
  205.  
  206.  
  207. PROCEDURE _mousewait
  208.  
  209. WHILE MOUSEKEY=0: WEND
  210.  
  211. END PROC
  212.  
  213. And that`s it, obviously the bigger the program we put in the procedure the
  214. more effective it becomes.  We can now call our procedure with:
  215.  
  216. Proc _mousewait
  217.  
  218. Or just:
  219.  
  220. _mousewait
  221.  
  222. And the procedure will be executed and return to the next command after the
  223. line we originally called the procedure from.  I could of called the 
  224. procedure anything I wanted but being descriptive helps later when your
  225. programs grow in size.
  226.  
  227. There are quite a few related commands to procedures but as usual I will just
  228. cover the ones we really need to know about for now, so here goes.
  229.  
  230. POP PROC
  231. ---------
  232. If you need to return from a procedure early for some reason this is your 
  233. man, an example could be:
  234.  
  235. IF A>5 then POP PROC
  236.  
  237. In other words if the A was more than five then end the procedure.  Don`t
  238. forget though that you still have to put END PROC at the END of your 
  239. PROCedure even if you do use POP PROC
  240.  
  241. The only other procedure related command we need to know about at the moment
  242. is:
  243.  
  244. GLOBAL
  245. ------
  246. When you put a program inside a procedure the procedure itself is a self
  247. contained module and any variables used inside that procedure will not
  248. effect any variables outside it even if they have the same name, unless that
  249. is, we assign them to be GLOBAL variables which means Amos will treat the 
  250. following variables the same inside and out of procedures, an example.
  251.  
  252. GLOBAL a$,team$(),beer(),f
  253.  
  254. This line makes a string called A$ an array called team$ an array called 
  255. beer and a lone variable called f all GLOBAL.  Notice that to make an array
  256. GLOBAL you must put () after it or GLOBAL will treat it as a normal variable.
  257.  
  258. OK that covers it on a need to know basis.  Now reload EXAMPLE17.Amos and you
  259. should understand the whole program a lot better.
  260.  
  261.                           End of chapter seventeen
  262.                           ^^^^^^^^^^^^^^^^^^^^^^^^
  263.